#!/usr/bin/perl 
use strict;
use DBI;
use CGI;
use TTraker;

my $dbh;
initialize_dbi();
my $form_ref = parse_input();
& print_header;


my %FORM_DATA = %$form_ref; 

my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $tmpFirst = $FORM_DATA{'tmpFirst'};
my $tmpLast = $FORM_DATA{'tmpLast'};
my $tmpLogin = $FORM_DATA{'tmpLogin'};
my $tmpLocation = $FORM_DATA{'tmpLocation'};
my $tmpFacility = $FORM_DATA{'tmpFacility'};
my $tmpAgency = $FORM_DATA{'tmpAgency'};
my $tmpRate = $FORM_DATA{'tmpRate'};


my ($info_ref, @info);


# print "$num, $name, $job, $loc, ENTERING: $tmpFirst, $tmpLast, $tmpLogin, $tmpLocation, $tmpFacility, $tmpAgency, $tmpRate**!<br/>";

my $message = "";
my $count = get_employee_count ($tmpLogin);
if ($tmpFirst eq "" || $tmpLast eq "" || $tmpLogin eq "" || $tmpLocation eq "" ||  $tmpFacility eq "" || $tmpAgency eq "" || $tmpRate eq "") {
	$message = "All fields must be filled in!";
	add_new_temp_msg ($num, $name, $job, $loc, $message);
} else {
	if  ($count == 0 ) {
		add_new_temp_employee ($tmpFirst, $tmpLast, $tmpLogin, $tmpLocation, $tmpFacility, $tmpAgency, $tmpRate);
		if ( $job eq "PAY" ) {
			pay_activity_screen_from_pm ($num, $name, $job, $loc, "");
		} else {
			sup_activity_screen_from_pm ($num, $name, $job, $loc, "");
		} 
	} elsif ( $count == 1) {
		$info_ref = get_employee_info ($tmpLogin);
		@info = @$info_ref;
		my $employee_num = $info[0];
		my $first_name = $info[1];
		my $last_name = $info[2];
		my $job = $info[3];
		my $location = $info[4];
		$message = "Employee #: $employee_num -  $first_name $last_name has login id $tmpLogin!<br/>
		Change the login id for $tmpFirst tmpLast or return to login";
		add_new_temp_msg ($num, $name, $job, $loc, $message);
		# print "$message<br/>";
	} else {
		print "More than one employee has the log in $tmpLogin!<br/>";
	}	
}

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub add_new_temp_msg
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $msg = $_[4];
print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_pay_add_new_client.pl</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  &nbsp&nbsp&nbsp&nbsp&nbsp
  <div id = "box5_1"> 
  <h3>Add New Temp Employee</h3>
  </div>
ENDHEADER

#<div id="box1">
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp First Name: <br/><br/>
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Last Name: <br/><br/>
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Login ID: <br/><br/>
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Agency: <br/><br/>
# </div>
print <<ENDHEADER;

<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_03_add_new_temp.pl" method="post">
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>  
	<table>
		<tr>
			<td>First Name: &nbsp</td>
			<td><input type="text" value = "$tmpFirst" size=24 name=tmpFirst />&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>Last Name: </td>
			<td><input type="text" value = "$tmpLast" size=24 name=tmpLast />&nbsp&nbsp</td>
		</tr>
		<tr>
		   <td>Login ID: </td>
		   <td><input type="text" value = "$tmpLogin" size=10 name=tmpLogin />&nbsp&nbsp</td>
		</tr>
		<tr>
			<td>Location: </td>
			<td><input type="text" value= "$tmpLocation" size=5 name=tmpLocation />&nbsp&nbsp</td>
		</tr>
		<tr>
   		<td>Facility #:</td>
   		<td><input type="text" value = "$tmpFacility" size=5 name=tmpFacility />&nbsp&nbsp</td>
   	<tr/>
   	<tr>
	   	<td>Agency: </td>
	   	<td><input type="text" value = "$tmpAgency" size=5 name=tmpAgency />&nbsp&nbsp</td>
		</tr>
		<tr>
   		<td>Rate: </td>
   		<td><input type="text" size=5 value = "$tmpRate" name=tmpRate />&nbsp&nbsp<td>
		</tr>
	</table>	
   	<br/>
   	<input type="submit" value="Add New Temp">
   	<br/><br/>
	<a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
 	<br/><br/>
 	<b id="warn">$msg</b><br/><br/>
 </form>
 </div>
 </body>
 </html>
ENDHEADER

}


